<?php

  if($faq == '')
    {
      $top5_cat_sql = "SELECT orbitfaq_stats.c_id, orbitfaq_categories.title, orbitfaq_stats.f_id, orbitfaq.title, COUNT(*) FROM orbitfaq, orbitfaq_stats, orbitfaq_categories WHERE (orbitfaq_stats.c_id = orbitfaq_categories.id) AND (orbitfaq_stats.f_id = orbitfaq.id) GROUP BY orbitfaq_stats.c_id ORDER BY COUNT(*) DESC LIMIT 5";
      $top5_recent_sql = "SELECT * FROM `orbitfaq_questions` WHERE a_id != '' AND `status` = '1' ORDER BY `posted_time` DESC LIMIT 5";
      $top5_rating_sql = 'SELECT orbitfaq_ratings.q_id, orbitfaq_questions.id, orbitfaq_questions.f_id, orbitfaq_questions.c_id, orbitfaq_questions.desc, COUNT(*) FROM orbitfaq_ratings, orbitfaq_questions WHERE (orbitfaq_ratings.q_id = orbitfaq_questions.id) GROUP BY orbitfaq_ratings.q_id ORDER BY COUNT(*) DESC LIMIT 5';
      $top5_users_sql = 'SELECT orbitfaq_ratings.u_id, orbitfaq_questions.f_id, orbitfaq_questions.posted_email, orbitfaq_users.id, orbitfaq_users.fullname, orbitfaq_users.login, COUNT(*) FROM orbitfaq_ratings, orbitfaq_questions,orbitfaq_users WHERE (orbitfaq_ratings.u_id = orbitfaq_users.id) OR (orbitfaq_questions.posted_email = orbitfaq_users.login) GROUP BY orbitfaq_users.id ORDER BY COUNT(*) DESC LIMIT 5';
    }
  else if($faq != '')
    {
      $top5_cat_sql = "SELECT orbitfaq_stats.c_id, orbitfaq_categories.title, orbitfaq_stats.f_id, orbitfaq.title, COUNT(*) FROM orbitfaq, orbitfaq_stats, orbitfaq_categories WHERE orbitfaq_stats.f_id = $faq AND (orbitfaq_stats.c_id = orbitfaq_categories.id) AND (orbitfaq_stats.f_id = orbitfaq.id) GROUP BY orbitfaq_stats.c_id ORDER BY COUNT(*) DESC LIMIT 5";
      $top5_recent_sql = "SELECT * FROM `orbitfaq_questions` WHERE a_id != '' AND `status` = '1' AND `f_id` = '$faq' ORDER BY `posted_time` DESC LIMIT 5";
      $top5_rating_sql = "SELECT orbitfaq_ratings.q_id, orbitfaq_questions.id, orbitfaq_questions.f_id, orbitfaq_questions.c_id, orbitfaq_questions.desc, COUNT(*) FROM orbitfaq_ratings, orbitfaq_questions WHERE orbitfaq_questions.f_id = '$faq' AND (orbitfaq_ratings.q_id = orbitfaq_questions.id) GROUP BY orbitfaq_ratings.q_id ORDER BY COUNT(*) DESC LIMIT 5";
      $top5_users_sql = 'SELECT orbitfaq_ratings.u_id, orbitfaq_questions.f_id, orbitfaq_questions.posted_email, orbitfaq_users.id, orbitfaq_users.fullname, orbitfaq_users.login, COUNT(*) FROM orbitfaq_ratings, orbitfaq_questions,orbitfaq_users WHERE (orbitfaq_ratings.u_id = orbitfaq_users.id) OR (orbitfaq_questions.posted_email = orbitfaq_users.login) GROUP BY orbitfaq_users.id ORDER BY COUNT(*) DESC LIMIT 5';
    }

    // Top 5 Most Categories
    $query_popcat = $top5_cat_sql;
    $result_popcat = $faqsql_query ($query_popcat)OR DIE( "$sql_query_error $query_popcat");

    while ($row_popcat = $faqsql_fetch_array ($result_popcat)){
      $popcat_id = $row_popcat[0];
      $popcat_title = $row_popcat[1];
      $popcat_fid = $row_popcat[2];
      $popcat_ftitle = $row_popcat[3];
      $popcat_count = $row_popcat[4];

      $popcat_title = htmlentities($popcat_title, ENT_QUOTES);

       $smarty_top5cat_array[] = array(
          "popcat_ftitle" => $popcat_ftitle,
          "popcat_title" => $popcat_title,
          "popcat_id" => $popcat_id,
          "popcat_fid" => $popcat_fid,
          "popcat_count" => $popcat_count
        );
    }

    // Top 5 Recent Questions
    $query_recent = $top5_recent_sql;
    $result_recent = $faqsql_query ($query_recent)OR DIE( "$sql_query_error $query_recent");

    while ($row_recent = $faqsql_fetch_array ($result_recent)){
      $recent_qid = $row_recent[0];
      $recent_fid = $row_recent[1];
      $recent_cid = $row_recent[2];
      $recent_qtitle = $row_recent[4];

      $recent_qtitle = htmlentities($recent_qtitle, ENT_QUOTES);

       $smarty_top5recent_array[] = array(
          "recent_qid" => $recent_qid,
          "recent_fid" => $recent_fid,
          "recent_cid" => $recent_cid,
          "recent_qtitle" => $recent_qtitle
        );
    }

    // Top 5 Most Rating Questions
    $query_rate = $top5_rating_sql;
    $result_rate = $faqsql_query ($query_rate)OR DIE( "$sql_query_error $query_rate");

    while ($row_rate = $faqsql_fetch_array ($result_rate)){
      $rate_qid = $row_rate[0];
      $rate_fid = $row_rate[2];
      $rate_cid = $row_rate[3];
      $rate_qtitle = $row_rate[4];
      $rate_count = $row_rate[5];

      $rate_qtitle = htmlentities($rate_qtitle, ENT_QUOTES);

       $smarty_top5rate_array[] = array(
          "rate_qid" => $rate_qid,
          "rate_fid" => $rate_fid,
          "rate_cid" => $rate_cid,
          "rate_qtitle" => $rate_qtitle,
          "rate_count" => $rate_count
        );
    }

    // Top 5 Most Active Users
    $query_users = $top5_users_sql;
    $result_users = $faqsql_query ($query_users)OR DIE( "$sql_query_error $query_users");

    while ($row_users = $faqsql_fetch_array ($result_users)){
      $users_uid = $row_users[0];
      $users_fid = $row_users[1];
      $users_posted_email = $row_users[2];
      $users_id = $row_users[3];
      $users_fullname = $row_users[4];
      $users_login = $row_users[5];
      $users_count = $row_users[6];

      $users_fullname = htmlentities($users_fullname, ENT_QUOTES);

       $smarty_top5users_array[] = array(
          "users_uid" => $users_uid,
          "users_fid" => $users_fid,
          "users_posted_email" => $users_posted_email,
          "users_id" => $users_id,
          "users_fullname" => $users_fullname,
          "users_login" => $users_login,
          "users_count" => $users_count
        );
    }


  // Categories
  $smarty->assign('Top5CatListingTitle','Top 5 Categories');
  $smarty->assign('Top5CatListing',$smarty_top5cat_array);

  // Recent Question
  $smarty->assign('Top5RecentListingTitle','Top 5 Most Recent Questions');
  $smarty->assign('Top5RecentListing',$smarty_top5recent_array);

  // Question Rating
  $smarty->assign('Top5RatingListingTitle','Top 5 Highest Rating Questions');
  $smarty->assign('Top5RatingListing',$smarty_top5rate_array);

  // Active User
  $smarty->assign('Top5UserListingTitle','Top 5 Most Active Users');
  $smarty->assign('Top5UserListing',$smarty_top5users_array);

?>